﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using ComLib.Extensions;
using ComLib.Entities;


namespace ComLib.Web.Lib.Controllers
{
    /// <summary>
    /// Controller Admin pages with automatic data binding from view to model(T).
    /// TODO: Need to write a custom Authorize attribute to avoid multiple EntityController(T) class
    /// that only vary based on authorize roles. The problem is that the attributes seem to be inherited
    /// and there cause issues when deriving the entitycontroller base classes.
    /// </summary>
    [HandleError]
    public class EntityControllerAdmin<T> : EntityControllerAdminBase<T>, IEntityController where T : class, IEntity, new()
    {

        [Authorize(Roles = "Admin")]
        public override ActionResult Create()
        {
            return base.Create();
        }


        [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Admin")]
        public virtual ActionResult Create(T entity)
        {
            return Create(entity, Conf.GetOrDefault<string>("CreateSuccessUrl", "Details"));
        }
    }



    /// <summary>
    /// TODO: Need to write a custom Authorize attribute to avoid multiple EntityController(T) class
    /// that only vary based on authorize roles. The problem is that the attributes seem to be inherited
    /// and there cause issues when deriving the entitycontroller base classes.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    [HandleError]
    public class EntityControllerAdminBase<T> : EntityControllerBase<T>, IEntityController where T : class, IEntity, new()
    {

        [Authorize(Roles = "Admin")]
        public override ActionResult Delete(int id)
        {
            return base.Delete(id);
        }

        
        [Authorize(Roles = "Admin")]
        public override ActionResult Delete(int id, string confirmButton)
        {
            return base.Delete(id, confirmButton);
        }
        
        
        [Authorize(Roles = "Admin")]
        public override ActionResult Edit(int id)
        {
            return base.Edit(id);
        }

        
        [Authorize(Roles = "Admin")]
        public override ActionResult Edit(int id, FormCollection collection)
        {
            return base.Edit(id, collection);
        }


        [Authorize(Roles = "Admin")]
        public override ActionResult Details(int id)
        {
            return base.Details(id);
        }


        [Authorize(Roles = "Admin")]
        public override ActionResult Index(int? page)
        {
            return base.Index(page);
        }


        [Authorize(Roles = "Admin")]
        public override ActionResult Manage(FormCollection formData)
        {
            return base.Manage(formData);
        }
    }
}
